<?php

namespace App\Models\Chat;

use App\Models\BaseModel;
use App\Models\ChatUsers;
use GatewayWorker\Lib\Gateway;
use Illuminate\Database\Eloquent\Model;


/**
 * Model ChatMessageModel
 *
 * @property int $id
 * @property int $fid
 * @property int $tid
 * @property string $msg
 * @property int $type
 * @property int $group_id
 *
 * @method static \Illuminate\Database\Query\Builder | \App\Models\Chat\ChatMessageModel where($column, $operator = null, $value = null, $boolean = 'and')
 * @method static \Illuminate\Database\Query\Builder | \App\Models\Chat\ChatMessageModel whereIn($column, $values, $boolean = 'and', $not = false)
 * @method static \Illuminate\Database\Query\Builder | \App\Models\Chat\ChatMessageModel leftJoin($table, $first, $operator = null, $second = null)
 * @method static \Illuminate\Database\Query\Builder | \App\Models\Chat\ChatMessageModel rightJoin($table, $first, $operator = null, $second = null)
 * @method static \Illuminate\Database\Query\Builder | \App\Models\Chat\ChatMessageModel get($columns = ['*'])
 * @method static \Illuminate\Database\Query\Builder | \App\Models\Chat\ChatMessageModel paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null)
 * @method static \Illuminate\Database\Query\Builder | \App\Models\Chat\ChatMessageModel find($id, $columns = ['*'])
 * @method static \Illuminate\Database\Query\Builder | \App\Models\Chat\ChatMessageModel first($columns = ['*'])
 * @method static \Illuminate\Database\Query\Builder | \App\Models\Chat\ChatMessageModel select($columns = ['*'])
 * @method static \Illuminate\Database\Query\Builder | \App\Models\Chat\ChatMessageModel orderBy($column, $direction = 'asc')
 * @package App\Model
 */

class ChatMessageModel extends BaseModel
{
    protected $table = 'chat_message';

    protected static function boot()
    {
        parent::boot();

        // 监听创建前事件
        static::creating(function ($chatMessage) {
            // 在这里执行创建前的操作

        });

        // 监听创建后事件
        static::created(function ($chatMessage) {
            // 在这里执行创建后的操作
//            Gateway::sendToUid($chatMessage->tid, json_encode([
//                'type' => 'msg',
//                'content' => $chatMessage->msg,
//                'data' => [
//                    'id' => $chatMessage->id,
//                    'fid' => $chatMessage->fid,
//                    'tid' => $chatMessage->tid,
//                    'msg' => $chatMessage->msg,
//                    'type' => $chatMessage->type,
//                    'group_id' => $chatMessage->group_id,
//                    'created_at' => $chatMessage->created_at,
//                ]
//            ]));
        });
    }

    public function filter($data)
    {
        //
        $where = [];
        if (isset($data['id']))
        {
            $where[] = ['id', '=', $data['id']];
        }
        if (isset($data['is_valid']))
        {
            $where[] = ['is_valid', '=', $data['is_valid']];
        }
        $this->getQueryByModel()->where($where);

        if ($this->getQueryByModel()->hasNamedScope("filter") && class_exists($this->getModelFilterClass())) {
            $this->getQueryByModel()->filter($data);
        }

        return $this->getQueryByModel();
    }

    public function tidInfo()
    {
        return $this->hasOne(ChatUsers::class,'id','tid')->select('id','nickname','avatar','mini_avatar');
    }
}
